/**
* require PreferenceStringContainer.js
*/ 

function DebugParamContainer() {
	this.construct();
}

DebugParamContainer.prototype = {
	defaultDebugParam	:	Array(Array("debug=true", "s.taobao.com")),
	debugParams	:	Array(),
	
	construct:	function(){
		//如果preference中的DebugParam为空，存入一个默认值
		this.getDebugParam();
		if(this.debugParams == "") {
			this.debugParams = this.defaultDebugParam;
			preferenceStringContainer.setStrings(this.debugParams, "debugparam");
		}
    },
	
	getDebugParam:	function() {
		this.debugParams = preferenceStringContainer.getStrings("debugparam");
	},

	addDebugParam:	function(debugParam, host, mywindow){
		if(!debugParam || !host){
            return false;
        }
        
        if(this.checkUniq(debugParam, host) == -1){
			this.debugParams.push(Array(debugParam, host));
			preferenceStringContainer.setStrings(this.debugParams, "debugparam");
			this.flush(mywindow);
            return true;
        }
        else{
            return false;
        }
	},
	
	removeDebugParam:	function(mywindow){
		var tree = mywindow.document.getElementById("debugParamTree");
		this.debugParams.splice(tree.currentIndex,1); 
		preferenceStringContainer.setStrings(this.debugParams, "debugparam");
		this.flush(mywindow);
	},
	
	checkUniq:	function(param, host){
		for(var i=0;i<this.debugParams.length;i++){
			if(this.debugParams[i][0] == param && this.debugParams[i][1] == host)
				return i;
		}
		return -1;
	},

	flush:	function(mywindow){
		this.getDebugParam();
	
		var treechildren = mywindow.document.getElementById("existingDebugParam");

		while(treechildren.childNodes.length>0){		
			treechildren.removeChild(treechildren.childNodes[0]);
		}

		for(var i=0;i<this.debugParams.length;i++){		
			var treeitem = mywindow.document.createElement("treeitem");
			var row = mywindow.document.createElement("treerow");
			
			var cell = mywindow.document.createElement('treecell');
			cell.setAttribute("label",this.debugParams[i][0]);
			row.appendChild(cell);
			
			var cell = mywindow.document.createElement('treecell');
			cell.setAttribute("label",this.debugParams[i][1]);
			row.appendChild(cell);
			
			treeitem.appendChild(row);
			treechildren.appendChild(treeitem);
		}
	}
};
